{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于歌曲相似度的推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "import sys\n",
    "reload(sys)\n",
    "sys.setdefaultencoding(\"utf-8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据\n",
    "用户(10万)、歌曲（3万）、播放次数、歌曲元数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "triplet_dataset_sub_song_merged=pd.read_csv('triplet_dataset_sub_song_merged.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOADQPP12A67020C82</td>\n",
       "      <td>12</td>\n",
       "      <td>You And Me Jesus</td>\n",
       "      <td>Tribute To Jake Hess</td>\n",
       "      <td>Jake Hess</td>\n",
       "      <td>2004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOANQFY12AB0183239</td>\n",
       "      <td>1</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAYATB12A6701FD50</td>\n",
       "      <td>1</td>\n",
       "      <td>Breakfast At Tiffany's</td>\n",
       "      <td>Home</td>\n",
       "      <td>Deep Blue Something</td>\n",
       "      <td>1993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBOAFP12A8C131F36</td>\n",
       "      <td>7</td>\n",
       "      <td>Lucky (Album Version)</td>\n",
       "      <td>We Sing.  We Dance.  We Steal Things.</td>\n",
       "      <td>Jason Mraz &amp; Colbie Caillat</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBONKR12A58A7A7E0</td>\n",
       "      <td>26</td>\n",
       "      <td>You're The One</td>\n",
       "      <td>If There Was A Way</td>\n",
       "      <td>Dwight Yoakam</td>\n",
       "      <td>1990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBZZDU12A6310D8A3</td>\n",
       "      <td>7</td>\n",
       "      <td>Don't Dream It's Over</td>\n",
       "      <td>Recurring Dream_ Best Of Crowded House (Domest...</td>\n",
       "      <td>Crowded House</td>\n",
       "      <td>1986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOCAHRT12A8C13A1A4</td>\n",
       "      <td>5</td>\n",
       "      <td>S.O.S.</td>\n",
       "      <td>SOS</td>\n",
       "      <td>Jonas Brothers</td>\n",
       "      <td>2007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODASIJ12A6D4F5D89</td>\n",
       "      <td>1</td>\n",
       "      <td>The Invisible Man</td>\n",
       "      <td>The Invisible Man</td>\n",
       "      <td>Michael Cretu</td>\n",
       "      <td>1985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODEAWL12AB0187032</td>\n",
       "      <td>8</td>\n",
       "      <td>American Idiot [feat. Green Day &amp; The Cast Of ...</td>\n",
       "      <td>The Original Broadway Cast Recording 'American...</td>\n",
       "      <td>Green Day</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  listen_count  \\\n",
       "0  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOADQPP12A67020C82            12   \n",
       "1  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAFTRR12AF72A8D4D             1   \n",
       "2  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOANQFY12AB0183239             1   \n",
       "3  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAYATB12A6701FD50             1   \n",
       "4  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBOAFP12A8C131F36             7   \n",
       "5  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBONKR12A58A7A7E0            26   \n",
       "6  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBZZDU12A6310D8A3             7   \n",
       "7  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOCAHRT12A8C13A1A4             5   \n",
       "8  d6589314c0a9bcbca4fee0c93b14bc402363afea  SODASIJ12A6D4F5D89             1   \n",
       "9  d6589314c0a9bcbca4fee0c93b14bc402363afea  SODEAWL12AB0187032             8   \n",
       "\n",
       "                                               title  \\\n",
       "0                                   You And Me Jesus   \n",
       "1                      Harder Better Faster Stronger   \n",
       "2                                           Uprising   \n",
       "3                             Breakfast At Tiffany's   \n",
       "4                              Lucky (Album Version)   \n",
       "5                                     You're The One   \n",
       "6                              Don't Dream It's Over   \n",
       "7                                             S.O.S.   \n",
       "8                                  The Invisible Man   \n",
       "9  American Idiot [feat. Green Day & The Cast Of ...   \n",
       "\n",
       "                                             release  \\\n",
       "0                               Tribute To Jake Hess   \n",
       "1                                          Discovery   \n",
       "2                                           Uprising   \n",
       "3                                               Home   \n",
       "4              We Sing.  We Dance.  We Steal Things.   \n",
       "5                                 If There Was A Way   \n",
       "6  Recurring Dream_ Best Of Crowded House (Domest...   \n",
       "7                                                SOS   \n",
       "8                                  The Invisible Man   \n",
       "9  The Original Broadway Cast Recording 'American...   \n",
       "\n",
       "                   artist_name  year  \n",
       "0                    Jake Hess  2004  \n",
       "1                    Daft Punk  2007  \n",
       "2                         Muse     0  \n",
       "3          Deep Blue Something  1993  \n",
       "4  Jason Mraz & Colbie Caillat     0  \n",
       "5                Dwight Yoakam  1990  \n",
       "6                Crowded House  1986  \n",
       "7               Jonas Brothers  2007  \n",
       "8                Michael Cretu  1985  \n",
       "9                    Green Day     0  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triplet_dataset_sub_song_merged.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10775785, 7)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triplet_dataset_sub_song_merged.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 子集抽取\n",
    "要计算用户的item与所有3万首歌的相似度太慢，可考虑只推荐最流行的5000首歌"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "song_count_df = pd.read_csv('song_playcount_df.csv')\n",
    "song_count_subset = song_count_df.head(n=5000)\n",
    "song_subset = list(song_count_subset.song)\n",
    "triplet_dataset_sub_song_merged_sub = triplet_dataset_sub_song_merged[triplet_dataset_sub_song_merged.song.isin(song_subset)]\n",
    "del triplet_dataset_sub_song_merged"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOADQPP12A67020C82</td>\n",
       "      <td>12</td>\n",
       "      <td>You And Me Jesus</td>\n",
       "      <td>Tribute To Jake Hess</td>\n",
       "      <td>Jake Hess</td>\n",
       "      <td>2004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOANQFY12AB0183239</td>\n",
       "      <td>1</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAYATB12A6701FD50</td>\n",
       "      <td>1</td>\n",
       "      <td>Breakfast At Tiffany's</td>\n",
       "      <td>Home</td>\n",
       "      <td>Deep Blue Something</td>\n",
       "      <td>1993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBOAFP12A8C131F36</td>\n",
       "      <td>7</td>\n",
       "      <td>Lucky (Album Version)</td>\n",
       "      <td>We Sing.  We Dance.  We Steal Things.</td>\n",
       "      <td>Jason Mraz &amp; Colbie Caillat</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  listen_count  \\\n",
       "0  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOADQPP12A67020C82            12   \n",
       "1  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAFTRR12AF72A8D4D             1   \n",
       "2  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOANQFY12AB0183239             1   \n",
       "3  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAYATB12A6701FD50             1   \n",
       "4  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBOAFP12A8C131F36             7   \n",
       "\n",
       "                           title                                release  \\\n",
       "0               You And Me Jesus                   Tribute To Jake Hess   \n",
       "1  Harder Better Faster Stronger                              Discovery   \n",
       "2                       Uprising                               Uprising   \n",
       "3         Breakfast At Tiffany's                                   Home   \n",
       "4          Lucky (Album Version)  We Sing.  We Dance.  We Steal Things.   \n",
       "\n",
       "                   artist_name  year  \n",
       "0                    Jake Hess  2004  \n",
       "1                    Daft Punk  2007  \n",
       "2                         Muse     0  \n",
       "3          Deep Blue Something  1993  \n",
       "4  Jason Mraz & Colbie Caillat     0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triplet_dataset_sub_song_merged_sub.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5535842, 7)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triplet_dataset_sub_song_merged_sub.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Item_CF_Recommenders import item_similarity_recommender_py\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里看到还有500多万的数据量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#分出百分之60作为训练数据，百分之40作为测试数据\n",
    "train_data, test_data = train_test_split(triplet_dataset_sub_song_merged_sub, test_size = 0.40, random_state=0)\n",
    "Item_model = item_similarity_recommender_py()#构建基于歌曲相似度推荐对象\n",
    "Item_model.fit(train_data, 'user', 'title')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>song</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Bad Company</td>\n",
       "      <td>0.022436</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Without Me</td>\n",
       "      <td>0.020601</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Hailie's Song</td>\n",
       "      <td>0.020327</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Superman</td>\n",
       "      <td>0.020234</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Mockingbird</td>\n",
       "      <td>0.019810</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Terre Promise</td>\n",
       "      <td>0.018715</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>The Real Slim Shady</td>\n",
       "      <td>0.018646</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>'Till I Collapse</td>\n",
       "      <td>0.018486</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>My Name Is</td>\n",
       "      <td>0.018424</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Bleed It Out [Live At Milton Keynes]</td>\n",
       "      <td>0.018304</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>One</td>\n",
       "      <td>0.017970</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Kryptonite</td>\n",
       "      <td>0.017948</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Sample Track 2</td>\n",
       "      <td>0.017895</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Here Without You</td>\n",
       "      <td>0.017442</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>My Immortal (Album Version)</td>\n",
       "      <td>0.017270</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Savior</td>\n",
       "      <td>0.017222</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>In The End (Album Version)</td>\n",
       "      <td>0.017194</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Everywhere I Go</td>\n",
       "      <td>0.017077</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>How You Remind Me</td>\n",
       "      <td>0.016784</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>Crawling (Album Version)</td>\n",
       "      <td>0.016633</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     user_id  \\\n",
       "0   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "1   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "2   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "3   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "4   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "5   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "6   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "7   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "8   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "9   f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "10  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "11  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "12  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "13  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "14  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "15  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "16  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "17  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "18  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "19  f65f96fa8c26780826e1f98c68e206caf0ef5eaa   \n",
       "\n",
       "                                    song     score rank  \n",
       "0                            Bad Company  0.022436    1  \n",
       "1                             Without Me  0.020601    2  \n",
       "2                          Hailie's Song  0.020327    3  \n",
       "3                               Superman  0.020234    4  \n",
       "4                            Mockingbird  0.019810    5  \n",
       "5                          Terre Promise  0.018715    6  \n",
       "6                    The Real Slim Shady  0.018646    7  \n",
       "7                       'Till I Collapse  0.018486    8  \n",
       "8                             My Name Is  0.018424    9  \n",
       "9   Bleed It Out [Live At Milton Keynes]  0.018304   10  \n",
       "10                                   One  0.017970   11  \n",
       "11                            Kryptonite  0.017948   12  \n",
       "12                        Sample Track 2  0.017895   13  \n",
       "13                      Here Without You  0.017442   14  \n",
       "14           My Immortal (Album Version)  0.017270   15  \n",
       "15                                Savior  0.017222   16  \n",
       "16            In The End (Album Version)  0.017194   17  \n",
       "17                       Everywhere I Go  0.017077   18  \n",
       "18                     How You Remind Me  0.016784   19  \n",
       "19              Crawling (Album Version)  0.016633   20  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_id = list(test_data.user)[7]\n",
    "#user_items = Item_model.predict(user_id)\n",
    "Item_model.predict(user_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4209921</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOYWBDC12A8C142D90</td>\n",
       "      <td>7</td>\n",
       "      <td>Acapulco 1922</td>\n",
       "      <td>The Lonely Bull</td>\n",
       "      <td>Herb Alpert And The Tijuana Brass</td>\n",
       "      <td>1962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209878</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOBPKPW12A6701E8F9</td>\n",
       "      <td>13</td>\n",
       "      <td>'Till I Collapse</td>\n",
       "      <td>The Eminem Show</td>\n",
       "      <td>Eminem / Nate Dogg</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209914</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOWBFFR12AF72A2AA1</td>\n",
       "      <td>3</td>\n",
       "      <td>Rock Bottom</td>\n",
       "      <td>The Slim Shady LP</td>\n",
       "      <td>Eminem</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209911</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOOHWRZ12AC468BA59</td>\n",
       "      <td>18</td>\n",
       "      <td>Genom tunna tyger</td>\n",
       "      <td>Prinsen av Peking</td>\n",
       "      <td>Markus KrunegÃ¥rd</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209895</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOHHJMK12AB018437E</td>\n",
       "      <td>26</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>Receive The Flame</td>\n",
       "      <td>Non</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209910</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOOHCLI12A6D4FBFE2</td>\n",
       "      <td>16</td>\n",
       "      <td>sillyworld (Album Version)</td>\n",
       "      <td>Come What[ever] May</td>\n",
       "      <td>Stone Sour</td>\n",
       "      <td>2006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209893</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOGVXGZ12A8C1333E3</td>\n",
       "      <td>128</td>\n",
       "      <td>Kiss You All Over</td>\n",
       "      <td>Greatest Hits</td>\n",
       "      <td>Exile</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209915</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOWBSGQ12A8C143163</td>\n",
       "      <td>7</td>\n",
       "      <td>Hits From The Bong</td>\n",
       "      <td>The Collection</td>\n",
       "      <td>Cypress Hill</td>\n",
       "      <td>1993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209903</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOKVZHI12A58A7A13E</td>\n",
       "      <td>20</td>\n",
       "      <td>Everywhere I Go</td>\n",
       "      <td>Swan Songs</td>\n",
       "      <td>Hollywood Undead</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209902</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOKQZYZ12A58A7767E</td>\n",
       "      <td>7</td>\n",
       "      <td>Ocean Avenue</td>\n",
       "      <td>Ocean Avenue</td>\n",
       "      <td>Yellowcard</td>\n",
       "      <td>2003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209917</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOWXDVT12B0B80BFA3</td>\n",
       "      <td>61</td>\n",
       "      <td>In Space</td>\n",
       "      <td>Melody AM</td>\n",
       "      <td>RÃ¶yksopp</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209879</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOBPRZE12A8C1313CC</td>\n",
       "      <td>2</td>\n",
       "      <td>Veleta (En Vivo Auditorio Nacional)</td>\n",
       "      <td>Lucero En Vivo  Auditorio Nacional</td>\n",
       "      <td>Lucero</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209896</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOHINQU12A67ADAF08</td>\n",
       "      <td>11</td>\n",
       "      <td>Road To Zion</td>\n",
       "      <td>Welcome to Jamrock</td>\n",
       "      <td>Damian Marley / Nas</td>\n",
       "      <td>2004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209912</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOQVIEV12A67AE2288</td>\n",
       "      <td>9</td>\n",
       "      <td>I'm Alive (Album Version)</td>\n",
       "      <td>Ten Thousand Fists</td>\n",
       "      <td>Disturbed</td>\n",
       "      <td>2005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209889</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOESWIW12AB017F1CD</td>\n",
       "      <td>17</td>\n",
       "      <td>Big Green Tractor</td>\n",
       "      <td>Wide Open</td>\n",
       "      <td>Jason Aldean</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             user                song  \\\n",
       "4209921  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOYWBDC12A8C142D90   \n",
       "4209878  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOBPKPW12A6701E8F9   \n",
       "4209914  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOWBFFR12AF72A2AA1   \n",
       "4209911  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOOHWRZ12AC468BA59   \n",
       "4209895  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOHHJMK12AB018437E   \n",
       "4209910  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOOHCLI12A6D4FBFE2   \n",
       "4209893  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOGVXGZ12A8C1333E3   \n",
       "4209915  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOWBSGQ12A8C143163   \n",
       "4209903  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOKVZHI12A58A7A13E   \n",
       "4209902  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOKQZYZ12A58A7767E   \n",
       "4209917  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOWXDVT12B0B80BFA3   \n",
       "4209879  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOBPRZE12A8C1313CC   \n",
       "4209896  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOHINQU12A67ADAF08   \n",
       "4209912  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOQVIEV12A67AE2288   \n",
       "4209889  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOESWIW12AB017F1CD   \n",
       "\n",
       "         listen_count                                title  \\\n",
       "4209921             7                        Acapulco 1922   \n",
       "4209878            13                     'Till I Collapse   \n",
       "4209914             3                          Rock Bottom   \n",
       "4209911            18                    Genom tunna tyger   \n",
       "4209895            26                              Spectre   \n",
       "4209910            16           sillyworld (Album Version)   \n",
       "4209893           128                    Kiss You All Over   \n",
       "4209915             7                   Hits From The Bong   \n",
       "4209903            20                      Everywhere I Go   \n",
       "4209902             7                         Ocean Avenue   \n",
       "4209917            61                             In Space   \n",
       "4209879             2  Veleta (En Vivo Auditorio Nacional)   \n",
       "4209896            11                         Road To Zion   \n",
       "4209912             9            I'm Alive (Album Version)   \n",
       "4209889            17                    Big Green Tractor   \n",
       "\n",
       "                                    release  \\\n",
       "4209921                     The Lonely Bull   \n",
       "4209878                     The Eminem Show   \n",
       "4209914                   The Slim Shady LP   \n",
       "4209911                   Prinsen av Peking   \n",
       "4209895                   Receive The Flame   \n",
       "4209910                 Come What[ever] May   \n",
       "4209893                       Greatest Hits   \n",
       "4209915                      The Collection   \n",
       "4209903                          Swan Songs   \n",
       "4209902                        Ocean Avenue   \n",
       "4209917                           Melody AM   \n",
       "4209879  Lucero En Vivo  Auditorio Nacional   \n",
       "4209896                  Welcome to Jamrock   \n",
       "4209912                  Ten Thousand Fists   \n",
       "4209889                           Wide Open   \n",
       "\n",
       "                               artist_name  year  \n",
       "4209921  Herb Alpert And The Tijuana Brass  1962  \n",
       "4209878                 Eminem / Nate Dogg  2002  \n",
       "4209914                             Eminem  1999  \n",
       "4209911                  Markus KrunegÃ¥rd  2009  \n",
       "4209895                                Non     0  \n",
       "4209910                         Stone Sour  2006  \n",
       "4209893                              Exile     0  \n",
       "4209915                       Cypress Hill  1993  \n",
       "4209903                   Hollywood Undead  2008  \n",
       "4209902                         Yellowcard  2003  \n",
       "4209917                          RÃ¶yksopp  2001  \n",
       "4209879                             Lucero     0  \n",
       "4209896                Damian Marley / Nas  2004  \n",
       "4209912                          Disturbed  2005  \n",
       "4209889                       Jason Aldean  2009  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data_user=test_data[test_data['user']==user_id]#找出该用户在测试集上听过的所有歌曲\n",
    "test_data_user.head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4209893</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOGVXGZ12A8C1333E3</td>\n",
       "      <td>128</td>\n",
       "      <td>Kiss You All Over</td>\n",
       "      <td>Greatest Hits</td>\n",
       "      <td>Exile</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209917</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOWXDVT12B0B80BFA3</td>\n",
       "      <td>61</td>\n",
       "      <td>In Space</td>\n",
       "      <td>Melody AM</td>\n",
       "      <td>RÃ¶yksopp</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209895</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOHHJMK12AB018437E</td>\n",
       "      <td>26</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>Receive The Flame</td>\n",
       "      <td>Non</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209903</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOKVZHI12A58A7A13E</td>\n",
       "      <td>20</td>\n",
       "      <td>Everywhere I Go</td>\n",
       "      <td>Swan Songs</td>\n",
       "      <td>Hollywood Undead</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209911</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOOHWRZ12AC468BA59</td>\n",
       "      <td>18</td>\n",
       "      <td>Genom tunna tyger</td>\n",
       "      <td>Prinsen av Peking</td>\n",
       "      <td>Markus KrunegÃ¥rd</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209889</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOESWIW12AB017F1CD</td>\n",
       "      <td>17</td>\n",
       "      <td>Big Green Tractor</td>\n",
       "      <td>Wide Open</td>\n",
       "      <td>Jason Aldean</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209910</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOOHCLI12A6D4FBFE2</td>\n",
       "      <td>16</td>\n",
       "      <td>sillyworld (Album Version)</td>\n",
       "      <td>Come What[ever] May</td>\n",
       "      <td>Stone Sour</td>\n",
       "      <td>2006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209878</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOBPKPW12A6701E8F9</td>\n",
       "      <td>13</td>\n",
       "      <td>'Till I Collapse</td>\n",
       "      <td>The Eminem Show</td>\n",
       "      <td>Eminem / Nate Dogg</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209896</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOHINQU12A67ADAF08</td>\n",
       "      <td>11</td>\n",
       "      <td>Road To Zion</td>\n",
       "      <td>Welcome to Jamrock</td>\n",
       "      <td>Damian Marley / Nas</td>\n",
       "      <td>2004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209912</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOQVIEV12A67AE2288</td>\n",
       "      <td>9</td>\n",
       "      <td>I'm Alive (Album Version)</td>\n",
       "      <td>Ten Thousand Fists</td>\n",
       "      <td>Disturbed</td>\n",
       "      <td>2005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209921</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOYWBDC12A8C142D90</td>\n",
       "      <td>7</td>\n",
       "      <td>Acapulco 1922</td>\n",
       "      <td>The Lonely Bull</td>\n",
       "      <td>Herb Alpert And The Tijuana Brass</td>\n",
       "      <td>1962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209915</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOWBSGQ12A8C143163</td>\n",
       "      <td>7</td>\n",
       "      <td>Hits From The Bong</td>\n",
       "      <td>The Collection</td>\n",
       "      <td>Cypress Hill</td>\n",
       "      <td>1993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209902</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOKQZYZ12A58A7767E</td>\n",
       "      <td>7</td>\n",
       "      <td>Ocean Avenue</td>\n",
       "      <td>Ocean Avenue</td>\n",
       "      <td>Yellowcard</td>\n",
       "      <td>2003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209914</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOWBFFR12AF72A2AA1</td>\n",
       "      <td>3</td>\n",
       "      <td>Rock Bottom</td>\n",
       "      <td>The Slim Shady LP</td>\n",
       "      <td>Eminem</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4209879</th>\n",
       "      <td>f65f96fa8c26780826e1f98c68e206caf0ef5eaa</td>\n",
       "      <td>SOBPRZE12A8C1313CC</td>\n",
       "      <td>2</td>\n",
       "      <td>Veleta (En Vivo Auditorio Nacional)</td>\n",
       "      <td>Lucero En Vivo  Auditorio Nacional</td>\n",
       "      <td>Lucero</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             user                song  \\\n",
       "4209893  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOGVXGZ12A8C1333E3   \n",
       "4209917  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOWXDVT12B0B80BFA3   \n",
       "4209895  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOHHJMK12AB018437E   \n",
       "4209903  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOKVZHI12A58A7A13E   \n",
       "4209911  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOOHWRZ12AC468BA59   \n",
       "4209889  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOESWIW12AB017F1CD   \n",
       "4209910  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOOHCLI12A6D4FBFE2   \n",
       "4209878  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOBPKPW12A6701E8F9   \n",
       "4209896  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOHINQU12A67ADAF08   \n",
       "4209912  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOQVIEV12A67AE2288   \n",
       "4209921  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOYWBDC12A8C142D90   \n",
       "4209915  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOWBSGQ12A8C143163   \n",
       "4209902  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOKQZYZ12A58A7767E   \n",
       "4209914  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOWBFFR12AF72A2AA1   \n",
       "4209879  f65f96fa8c26780826e1f98c68e206caf0ef5eaa  SOBPRZE12A8C1313CC   \n",
       "\n",
       "         listen_count                                title  \\\n",
       "4209893           128                    Kiss You All Over   \n",
       "4209917            61                             In Space   \n",
       "4209895            26                              Spectre   \n",
       "4209903            20                      Everywhere I Go   \n",
       "4209911            18                    Genom tunna tyger   \n",
       "4209889            17                    Big Green Tractor   \n",
       "4209910            16           sillyworld (Album Version)   \n",
       "4209878            13                     'Till I Collapse   \n",
       "4209896            11                         Road To Zion   \n",
       "4209912             9            I'm Alive (Album Version)   \n",
       "4209921             7                        Acapulco 1922   \n",
       "4209915             7                   Hits From The Bong   \n",
       "4209902             7                         Ocean Avenue   \n",
       "4209914             3                          Rock Bottom   \n",
       "4209879             2  Veleta (En Vivo Auditorio Nacional)   \n",
       "\n",
       "                                    release  \\\n",
       "4209893                       Greatest Hits   \n",
       "4209917                           Melody AM   \n",
       "4209895                   Receive The Flame   \n",
       "4209903                          Swan Songs   \n",
       "4209911                   Prinsen av Peking   \n",
       "4209889                           Wide Open   \n",
       "4209910                 Come What[ever] May   \n",
       "4209878                     The Eminem Show   \n",
       "4209896                  Welcome to Jamrock   \n",
       "4209912                  Ten Thousand Fists   \n",
       "4209921                     The Lonely Bull   \n",
       "4209915                      The Collection   \n",
       "4209902                        Ocean Avenue   \n",
       "4209914                   The Slim Shady LP   \n",
       "4209879  Lucero En Vivo  Auditorio Nacional   \n",
       "\n",
       "                               artist_name  year  \n",
       "4209893                              Exile     0  \n",
       "4209917                          RÃ¶yksopp  2001  \n",
       "4209895                                Non     0  \n",
       "4209903                   Hollywood Undead  2008  \n",
       "4209911                  Markus KrunegÃ¥rd  2009  \n",
       "4209889                       Jason Aldean  2009  \n",
       "4209910                         Stone Sour  2006  \n",
       "4209878                 Eminem / Nate Dogg  2002  \n",
       "4209896                Damian Marley / Nas  2004  \n",
       "4209912                          Disturbed  2005  \n",
       "4209921  Herb Alpert And The Tijuana Brass  1962  \n",
       "4209915                       Cypress Hill  1993  \n",
       "4209902                         Yellowcard  2003  \n",
       "4209914                             Eminem  1999  \n",
       "4209879                             Lucero     0  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#按总播放次数排序\n",
    "test_data_user_sort = test_data_user.sort_values(by = 'listen_count', ascending = False)\n",
    "test_data_user_sort.head(n =20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里把测试集上用户点击的歌曲的目录和播放次数进行排序当作用户感兴趣的顺序。\n",
    "由于计算时间过长这里，这里值计算一个用户。\n",
    "推荐的20首歌当中，有如下几首用户同样感兴趣,分别是:'Till I Collapse,Everywhere I Go两首歌。\n",
    "针对测试集上的用户f65f96fa8c26780826e1f98c68e206caf0ef5eaa的准确度为:Precision=2/20=0.1=10%\n",
    "召回率为:Recall=2/15=13.33%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_id = list(test_data.user)[0]\n",
    "user_items = Item_model.predict(user_id)\n",
    "#Item_model.predict(user_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5232551</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOSFAQM12AB018427B</td>\n",
       "      <td>1</td>\n",
       "      <td>Solo Dolo (nightmare)</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232536</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOIBMQT12AB0181395</td>\n",
       "      <td>18</td>\n",
       "      <td>Clobberin' Time</td>\n",
       "      <td>Live In A Dive</td>\n",
       "      <td>Sick Of It All</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232521</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOCNAXF12A6D4F9B34</td>\n",
       "      <td>14</td>\n",
       "      <td>Creil City</td>\n",
       "      <td>Fat Come Back</td>\n",
       "      <td>Alliance Ethnik</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232540</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOKJBXF12A58A7CD00</td>\n",
       "      <td>1</td>\n",
       "      <td>Ink My Whole Body</td>\n",
       "      <td>Ink My Whole Body</td>\n",
       "      <td>Wiz Khalifa</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232523</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SODRFRJ12A8C144167</td>\n",
       "      <td>8</td>\n",
       "      <td>Airplanes</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232544</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOMBNVR12AF72A736E</td>\n",
       "      <td>1</td>\n",
       "      <td>Why Georgia</td>\n",
       "      <td>Where The Light Is: John Mayer Live In Los Ang...</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232529</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOEPWYH12AF72A4813</td>\n",
       "      <td>1</td>\n",
       "      <td>Cudi Zone</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232518</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOASTSF12A8C1420B5</td>\n",
       "      <td>14</td>\n",
       "      <td>Welk</td>\n",
       "      <td>Dolores</td>\n",
       "      <td>Bohren &amp; Der Club Of Gore</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232549</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOPIEIJ12A6D4F8F81</td>\n",
       "      <td>1</td>\n",
       "      <td>Waterfall</td>\n",
       "      <td>Ã La Carte</td>\n",
       "      <td>Triumvirat</td>\n",
       "      <td>1978</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232517</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOARUPP12AB01842E0</td>\n",
       "      <td>1</td>\n",
       "      <td>Up Up &amp; Away</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232552</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOTHABI12A58A7DACB</td>\n",
       "      <td>1</td>\n",
       "      <td>Perfectly Lonely</td>\n",
       "      <td>Battle Studies</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232519</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOBDRND12A8C13FD08</td>\n",
       "      <td>12</td>\n",
       "      <td>Sun Hands</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232546</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOMMIUM12A8C13FD49</td>\n",
       "      <td>28</td>\n",
       "      <td>World News</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232542</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOLOVPR12AB0182D03</td>\n",
       "      <td>14</td>\n",
       "      <td>Shape Shifter</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             user                song  \\\n",
       "5232551  050834fcdba55aa3e6ada1d4f2949002ee817254  SOSFAQM12AB018427B   \n",
       "5232536  050834fcdba55aa3e6ada1d4f2949002ee817254  SOIBMQT12AB0181395   \n",
       "5232521  050834fcdba55aa3e6ada1d4f2949002ee817254  SOCNAXF12A6D4F9B34   \n",
       "5232540  050834fcdba55aa3e6ada1d4f2949002ee817254  SOKJBXF12A58A7CD00   \n",
       "5232523  050834fcdba55aa3e6ada1d4f2949002ee817254  SODRFRJ12A8C144167   \n",
       "5232544  050834fcdba55aa3e6ada1d4f2949002ee817254  SOMBNVR12AF72A736E   \n",
       "5232529  050834fcdba55aa3e6ada1d4f2949002ee817254  SOEPWYH12AF72A4813   \n",
       "5232518  050834fcdba55aa3e6ada1d4f2949002ee817254  SOASTSF12A8C1420B5   \n",
       "5232549  050834fcdba55aa3e6ada1d4f2949002ee817254  SOPIEIJ12A6D4F8F81   \n",
       "5232517  050834fcdba55aa3e6ada1d4f2949002ee817254  SOARUPP12AB01842E0   \n",
       "5232552  050834fcdba55aa3e6ada1d4f2949002ee817254  SOTHABI12A58A7DACB   \n",
       "5232519  050834fcdba55aa3e6ada1d4f2949002ee817254  SOBDRND12A8C13FD08   \n",
       "5232546  050834fcdba55aa3e6ada1d4f2949002ee817254  SOMMIUM12A8C13FD49   \n",
       "5232542  050834fcdba55aa3e6ada1d4f2949002ee817254  SOLOVPR12AB0182D03   \n",
       "\n",
       "         listen_count                  title  \\\n",
       "5232551             1  Solo Dolo (nightmare)   \n",
       "5232536            18        Clobberin' Time   \n",
       "5232521            14             Creil City   \n",
       "5232540             1      Ink My Whole Body   \n",
       "5232523             8              Airplanes   \n",
       "5232544             1            Why Georgia   \n",
       "5232529             1              Cudi Zone   \n",
       "5232518            14                   Welk   \n",
       "5232549             1              Waterfall   \n",
       "5232517             1           Up Up & Away   \n",
       "5232552             1       Perfectly Lonely   \n",
       "5232519            12              Sun Hands   \n",
       "5232546            28             World News   \n",
       "5232542            14          Shape Shifter   \n",
       "\n",
       "                                                   release  \\\n",
       "5232551                    Man On The Moon: The End Of Day   \n",
       "5232536                                     Live In A Dive   \n",
       "5232521                                      Fat Come Back   \n",
       "5232540                                  Ink My Whole Body   \n",
       "5232523                                      Gorilla Manor   \n",
       "5232544  Where The Light Is: John Mayer Live In Los Ang...   \n",
       "5232529                    Man On The Moon: The End Of Day   \n",
       "5232518                                            Dolores   \n",
       "5232549                                        Ã La Carte   \n",
       "5232517                    Man On The Moon: The End Of Day   \n",
       "5232552                                     Battle Studies   \n",
       "5232519                                      Gorilla Manor   \n",
       "5232546                                      Gorilla Manor   \n",
       "5232542                                      Gorilla Manor   \n",
       "\n",
       "                       artist_name  year  \n",
       "5232551                   Kid Cudi  2009  \n",
       "5232536             Sick Of It All  2002  \n",
       "5232521            Alliance Ethnik  1999  \n",
       "5232540                Wiz Khalifa  2010  \n",
       "5232523              Local Natives  2009  \n",
       "5232544                 John Mayer     0  \n",
       "5232529                   Kid Cudi  2009  \n",
       "5232518  Bohren & Der Club Of Gore  2008  \n",
       "5232549                 Triumvirat  1978  \n",
       "5232517                   Kid Cudi  2009  \n",
       "5232552                 John Mayer     0  \n",
       "5232519              Local Natives  2009  \n",
       "5232546              Local Natives  2009  \n",
       "5232542              Local Natives  2009  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data_user=test_data[test_data['user']==user_id]#找出该用户在测试集上听过的所有歌曲\n",
    "test_data_user.head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5232546</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOMMIUM12A8C13FD49</td>\n",
       "      <td>28</td>\n",
       "      <td>World News</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232536</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOIBMQT12AB0181395</td>\n",
       "      <td>18</td>\n",
       "      <td>Clobberin' Time</td>\n",
       "      <td>Live In A Dive</td>\n",
       "      <td>Sick Of It All</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232521</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOCNAXF12A6D4F9B34</td>\n",
       "      <td>14</td>\n",
       "      <td>Creil City</td>\n",
       "      <td>Fat Come Back</td>\n",
       "      <td>Alliance Ethnik</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232518</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOASTSF12A8C1420B5</td>\n",
       "      <td>14</td>\n",
       "      <td>Welk</td>\n",
       "      <td>Dolores</td>\n",
       "      <td>Bohren &amp; Der Club Of Gore</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232542</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOLOVPR12AB0182D03</td>\n",
       "      <td>14</td>\n",
       "      <td>Shape Shifter</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232519</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOBDRND12A8C13FD08</td>\n",
       "      <td>12</td>\n",
       "      <td>Sun Hands</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232523</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SODRFRJ12A8C144167</td>\n",
       "      <td>8</td>\n",
       "      <td>Airplanes</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232551</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOSFAQM12AB018427B</td>\n",
       "      <td>1</td>\n",
       "      <td>Solo Dolo (nightmare)</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232540</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOKJBXF12A58A7CD00</td>\n",
       "      <td>1</td>\n",
       "      <td>Ink My Whole Body</td>\n",
       "      <td>Ink My Whole Body</td>\n",
       "      <td>Wiz Khalifa</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232544</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOMBNVR12AF72A736E</td>\n",
       "      <td>1</td>\n",
       "      <td>Why Georgia</td>\n",
       "      <td>Where The Light Is: John Mayer Live In Los Ang...</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232529</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOEPWYH12AF72A4813</td>\n",
       "      <td>1</td>\n",
       "      <td>Cudi Zone</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232549</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOPIEIJ12A6D4F8F81</td>\n",
       "      <td>1</td>\n",
       "      <td>Waterfall</td>\n",
       "      <td>Ã La Carte</td>\n",
       "      <td>Triumvirat</td>\n",
       "      <td>1978</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232517</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOARUPP12AB01842E0</td>\n",
       "      <td>1</td>\n",
       "      <td>Up Up &amp; Away</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232552</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOTHABI12A58A7DACB</td>\n",
       "      <td>1</td>\n",
       "      <td>Perfectly Lonely</td>\n",
       "      <td>Battle Studies</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             user                song  \\\n",
       "5232546  050834fcdba55aa3e6ada1d4f2949002ee817254  SOMMIUM12A8C13FD49   \n",
       "5232536  050834fcdba55aa3e6ada1d4f2949002ee817254  SOIBMQT12AB0181395   \n",
       "5232521  050834fcdba55aa3e6ada1d4f2949002ee817254  SOCNAXF12A6D4F9B34   \n",
       "5232518  050834fcdba55aa3e6ada1d4f2949002ee817254  SOASTSF12A8C1420B5   \n",
       "5232542  050834fcdba55aa3e6ada1d4f2949002ee817254  SOLOVPR12AB0182D03   \n",
       "5232519  050834fcdba55aa3e6ada1d4f2949002ee817254  SOBDRND12A8C13FD08   \n",
       "5232523  050834fcdba55aa3e6ada1d4f2949002ee817254  SODRFRJ12A8C144167   \n",
       "5232551  050834fcdba55aa3e6ada1d4f2949002ee817254  SOSFAQM12AB018427B   \n",
       "5232540  050834fcdba55aa3e6ada1d4f2949002ee817254  SOKJBXF12A58A7CD00   \n",
       "5232544  050834fcdba55aa3e6ada1d4f2949002ee817254  SOMBNVR12AF72A736E   \n",
       "5232529  050834fcdba55aa3e6ada1d4f2949002ee817254  SOEPWYH12AF72A4813   \n",
       "5232549  050834fcdba55aa3e6ada1d4f2949002ee817254  SOPIEIJ12A6D4F8F81   \n",
       "5232517  050834fcdba55aa3e6ada1d4f2949002ee817254  SOARUPP12AB01842E0   \n",
       "5232552  050834fcdba55aa3e6ada1d4f2949002ee817254  SOTHABI12A58A7DACB   \n",
       "\n",
       "         listen_count                  title  \\\n",
       "5232546            28             World News   \n",
       "5232536            18        Clobberin' Time   \n",
       "5232521            14             Creil City   \n",
       "5232518            14                   Welk   \n",
       "5232542            14          Shape Shifter   \n",
       "5232519            12              Sun Hands   \n",
       "5232523             8              Airplanes   \n",
       "5232551             1  Solo Dolo (nightmare)   \n",
       "5232540             1      Ink My Whole Body   \n",
       "5232544             1            Why Georgia   \n",
       "5232529             1              Cudi Zone   \n",
       "5232549             1              Waterfall   \n",
       "5232517             1           Up Up & Away   \n",
       "5232552             1       Perfectly Lonely   \n",
       "\n",
       "                                                   release  \\\n",
       "5232546                                      Gorilla Manor   \n",
       "5232536                                     Live In A Dive   \n",
       "5232521                                      Fat Come Back   \n",
       "5232518                                            Dolores   \n",
       "5232542                                      Gorilla Manor   \n",
       "5232519                                      Gorilla Manor   \n",
       "5232523                                      Gorilla Manor   \n",
       "5232551                    Man On The Moon: The End Of Day   \n",
       "5232540                                  Ink My Whole Body   \n",
       "5232544  Where The Light Is: John Mayer Live In Los Ang...   \n",
       "5232529                    Man On The Moon: The End Of Day   \n",
       "5232549                                        Ã La Carte   \n",
       "5232517                    Man On The Moon: The End Of Day   \n",
       "5232552                                     Battle Studies   \n",
       "\n",
       "                       artist_name  year  \n",
       "5232546              Local Natives  2009  \n",
       "5232536             Sick Of It All  2002  \n",
       "5232521            Alliance Ethnik  1999  \n",
       "5232518  Bohren & Der Club Of Gore  2008  \n",
       "5232542              Local Natives  2009  \n",
       "5232519              Local Natives  2009  \n",
       "5232523              Local Natives  2009  \n",
       "5232551                   Kid Cudi  2009  \n",
       "5232540                Wiz Khalifa  2010  \n",
       "5232544                 John Mayer     0  \n",
       "5232529                   Kid Cudi  2009  \n",
       "5232549                 Triumvirat  1978  \n",
       "5232517                   Kid Cudi  2009  \n",
       "5232552                 John Mayer     0  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#按总播放次数排序\n",
    "test_data_user_sort = test_data_user.sort_values(by = 'listen_count', ascending = False)\n",
    "test_data_user_sort.head(n =20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>song</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Shape Shifter</td>\n",
       "      <td>0.078750</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>World News</td>\n",
       "      <td>0.064099</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Airplanes</td>\n",
       "      <td>0.062584</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Sun Hands</td>\n",
       "      <td>0.060408</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Creil City</td>\n",
       "      <td>0.041069</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Simple As...</td>\n",
       "      <td>0.039124</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Up Up &amp; Away</td>\n",
       "      <td>0.038457</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Solo Dolo (nightmare)</td>\n",
       "      <td>0.037854</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Cudi Zone</td>\n",
       "      <td>0.037785</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>ReprÃ©sente</td>\n",
       "      <td>0.035138</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Pursuit Of Happiness (nightmare)</td>\n",
       "      <td>0.030386</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Schwarze Biene (Black Maja)</td>\n",
       "      <td>0.029253</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Heartbreak Warfare</td>\n",
       "      <td>0.028377</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Welk</td>\n",
       "      <td>0.027512</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>The Unforgiven III</td>\n",
       "      <td>0.027358</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Drop The World</td>\n",
       "      <td>0.024097</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Electric Feel</td>\n",
       "      <td>0.023781</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Breakeven</td>\n",
       "      <td>0.022942</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>Teach Me How To Dougie</td>\n",
       "      <td>0.022607</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>The Funeral (Album Version)</td>\n",
       "      <td>0.022584</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     user_id  \\\n",
       "0   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "1   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "2   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "3   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "4   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "5   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "6   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "7   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "8   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "9   050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "10  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "11  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "12  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "13  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "14  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "15  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "16  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "17  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "18  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "19  050834fcdba55aa3e6ada1d4f2949002ee817254   \n",
       "\n",
       "                                song     score rank  \n",
       "0                      Shape Shifter  0.078750    1  \n",
       "1                         World News  0.064099    2  \n",
       "2                          Airplanes  0.062584    3  \n",
       "3                          Sun Hands  0.060408    4  \n",
       "4                         Creil City  0.041069    5  \n",
       "5                       Simple As...  0.039124    6  \n",
       "6                       Up Up & Away  0.038457    7  \n",
       "7              Solo Dolo (nightmare)  0.037854    8  \n",
       "8                          Cudi Zone  0.037785    9  \n",
       "9                        ReprÃ©sente  0.035138   10  \n",
       "10  Pursuit Of Happiness (nightmare)  0.030386   11  \n",
       "11       Schwarze Biene (Black Maja)  0.029253   12  \n",
       "12                Heartbreak Warfare  0.028377   13  \n",
       "13                              Welk  0.027512   14  \n",
       "14                The Unforgiven III  0.027358   15  \n",
       "15                    Drop The World  0.024097   16  \n",
       "16                     Electric Feel  0.023781   17  \n",
       "17                         Breakeven  0.022942   18  \n",
       "18            Teach Me How To Dougie  0.022607   19  \n",
       "19       The Funeral (Album Version)  0.022584   20  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_items"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更换用户之后推荐的歌曲有以下:Shape Shifter,World News,Airplanes,Sun Hands,Creil City ,Up Up & Away,Solo Dolo (nightmare),Cudi Zone,Welk.\n",
    "针对测试集上的用户050834fcdba55aa3e6ada1d4f2949002ee817254的准确度为:Precision=9/20=0.45=45% 召回率为:Recall=9/14=64.29%\n",
    "这两个用户差别比较大，可能是拆分数据集的时候对应用户的曲目分离的不是很精确导致的。\n",
    "基于协同过滤的推荐方式计算量特别大。这里只随即选取一个用户做测试。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  },
  "toc": {
   "nav_menu": {
    "height": "153px",
    "width": "160px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": true,
   "toc_position": {
    "height": "691px",
    "left": "0px",
    "right": "1405px",
    "top": "106px",
    "width": "303px"
   },
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
